package com.aliyun.heiyu.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @Description 日期时间工具类
 * @Author luo
 * @Date 2020/1/22 19:09
 **/
public class DateUtil {
    private final static String DATE_REGEX = "^([1-9]\\d{3}-)(([0]{0,1}[1-9]-)|([1][0-2]-))(([0-3]{0,1}[0-9]))$";
    private  final static String REG_EXP_DATE = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3])-([0-5][0-9])-([0-5][0-9])$";
    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat hms = new SimpleDateFormat("HH:mm:ss");
    public static final String HOURMINUTE_PATTERN = "HH:mm";
    private final static SimpleDateFormat yyyy = new SimpleDateFormat("yyyyy");

    /**
     * @return返回微秒
     */
    public static Long getmicTime() {
        // 微秒
        Long cutime = System.currentTimeMillis() * 1000;
        // 纳秒
        Long nanoTime = System.nanoTime();
        cutime = cutime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
        return cutime;
    }

    /*日期转yyyy-MM-dd HH:mm:ss 格式字符串*/
    public static String getStringDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String dateToString(Date date){
        String dateString="";
        if(null==date){
            return dateString;
        }
        try {
            return sdf.format(date);
        } catch (Exception e) {
            return dateString;
        }
    }

    /**
     * datetime型字符串 yyyy-MM-dd HH:mm:ss
     *   转日期
     */
    public static Date stringToDate(String data){
        try {
            return sdf.parse(data);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     * @param strDate
     */
    public static Date shortStringToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static Timestamp string2Stamp(String str, SimpleDateFormat sdf) {
        if(str == null || str.isEmpty()){
            return null;
        }
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(date == null){
            return null;
        }
        return new Timestamp(date.getTime());
    }

    public static String stamp2String(Timestamp stamp, SimpleDateFormat sdf) {
        if(stamp == null){
            return "";
        }
        return sdf.format(stamp);
    }

    public static String millisecondToDate(Long millisecond){
        Date date = new Date(millisecond);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static long dateToMillisecond(String dateTime) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  calendar.getTimeInMillis();
    }
    public static String getYearFirstString(String date) {
        if(StringUtil.isEmpty(date)){
            return null;
        }
        String mmdd = "-01-01";
        return date+mmdd;
    }

    public static String dateToHms(Date date) {
        try {
            return hms.format(date);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取多少天之前的日期
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取年
     * @param date
     * @return
     */
    public static String getYYYY(String date) {
        if (null == date && date.length() < 4) {
            return null;
        }
        return date.substring(0,4);
    }

    /**
     * 在给定的日期加上或减去指定月份后的日期
     * @param sourceDate 原始时间
     * @param month      要调整的月份，向前为负数，向后为正数
     * @return
     */
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);

        return c.getTime();
    }

    public static boolean isEffectiveDate(String nowDate, String startDate, String endDate) {
        try {
            Date startTime = hms.parse(startDate);
            Date endTime = hms.parse(endDate);
            Date nowTime = hms.parse(nowDate);

            if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
                return true;
            }
            long now = nowTime.getTime();
            long begin = startTime.getTime();
            long end = endTime.getTime();

            if (now > begin && now < end) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 年月日校验
     * @param datestr
     * @return
     */
    public static boolean validDateEffecitive(String datestr){
        boolean matches =  Pattern.matches(DATE_REGEX, datestr);
        if(!matches){
            return matches;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //设置日期格式转的严谨性
        sdf.setLenient(false);
        try {
            sdf.parse(datestr);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return matches;
    }

    /**
     * 年月日时分秒校验
     * @param datestr
     * @return
     */
    public static boolean validDateTimeEffecitive(String datestr){
      if(StringUtil.isEmpty(datestr)){
          return false;
      }
        //设置日期格式转的严谨性
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setLenient(false);
        try {
            sdf.parse(datestr);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static boolean isLegalDate(String sDate) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }

}
